package com.city.java.products.controller;

import com.city.java.base.utils.PageVO;
import com.city.java.products.pojo.entity.Products;
import com.city.java.products.service.ProductService;
import com.city.java.base.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品控制器
 * RestController注解: 组合注解，相当于@Controller + @ResponseBody
 * Api注解: Knife4j文档中模块的显示名称
 * RequestMapping注解: 设置统一的请求路径前缀
 */
@Slf4j
@Api(tags = "05.商品管理模块")
@RestController
@RequestMapping("/api/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 添加商品
     * PostMapping注解: 处理POST请求
     * ApiOperation注解: Knife4j文档中接口的显示名称
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加商品", notes = "添加新商品信息")
    public JsonResult addProduct(
            @ApiParam(value = "商品信息", required = true)
            @RequestBody Products product) {
        log.info("接收到添加商品请求: {}", product);
        return productService.addProduct(product);
    }

    /**
     * 根据商品ID查询商品
     * GetMapping注解: 处理GET请求
     * PathVariable注解: 获取路径中的参数
     */
    @GetMapping("/select/id/{productId}")
    @ApiOperation(value = "根据ID查询商品", notes = "根据商品ID查询商品详细信息")
    public JsonResult getProductById(
            @ApiParam(value = "商品ID", required = true, example = "1")
            @PathVariable Long productId) {
        log.info("接收到根据ID查询商品请求: ID={}", productId);
        return productService.getProductById(productId);
    }

    /**
     * 查询所有商品列表
     */
    @GetMapping("/quarry")
    @ApiOperation(value = "查询所有商品", notes = "查询所有商品列表")
    public JsonResult getAllProducts() {
        log.info("接收到查询所有商品请求");
        return productService.getAllProducts();
    }

    /**
     * 根据积分范围查询商品列表
     */
    @GetMapping("/select/points")
    @ApiOperation(value = "根据积分范围查询商品", notes = "根据积分范围查询商品列表")
    public JsonResult getProductsByPointsRange(
            @ApiParam(value = "最小积分", example = "0")
            @RequestParam(required = false) Integer minPoints,
            @ApiParam(value = "最大积分", example = "1000")
            @RequestParam(required = false) Integer maxPoints) {
        log.info("接收到根据积分范围查询商品请求: minPoints={}, maxPoints={}", minPoints, maxPoints);
        return productService.getProductsByPointsRange(minPoints, maxPoints);
    }

    /**
     * 查询商品列表
     */
    @GetMapping("/select")
    @ApiOperation(value = "根据积分范围查询商品", notes = "根据积分范围查询商品列表")
    public JsonResult getProductsByPointsRange(Products products) {
        log.debug("接受到的products数据：{}",products);
        PageVO<Products> pageVO = productService.select(products);
        return JsonResult.ok(pageVO);
    }

    /**
     * 根据商品名称查询商品列表
     */
    @GetMapping("/select/name")
    @ApiOperation(value = "根据商品名称查询商品", notes = "根据商品名称查询商品列表")
    public JsonResult getProductsByName(
            @ApiParam(value = "商品名称", required = true, example = "洗衣液")
            @RequestParam String productName) {
        log.info("接收到根据商品名称查询商品请求: productName={}", productName);
        return productService.getProductsByName(productName);
    }

    /**
     * 根据商品ID删除商品
     * DeleteMapping注解: 处理DELETE请求
     */
    @DeleteMapping("/delete/id/{productId}")
    @ApiOperation(value = "根据ID删除商品", notes = "根据商品ID删除商品")
    public JsonResult deleteProductById(
            @ApiParam(value = "商品ID", required = true, example = "1")
            @PathVariable Long productId) {
        log.info("接收到根据ID删除商品请求: ID={}", productId);
        return productService.deleteProductById(productId);
    }

    /**
     * 更新商品信息
     * PutMapping注解: 处理PUT请求
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新商品信息", notes = "更新商品详细信息")
    public JsonResult updateProduct(
            @ApiParam(value = "商品信息", required = true)
            @RequestBody Products product) {
        log.info("接收到更新商品信息请求: {}", product);
        return productService.updateProduct(product);
    }
}
